package com.zzyl.serve.service.impl;

import java.util.List;

import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String CACHE_KEY_PREFIX = "nursingProject:all";
    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {

        return getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        boolean saves = save(nursingProject);
        if(saves){
            deleteCache();
        }
        return saves ? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {

        boolean b = updateById(nursingProject);
        if(b){
            deleteCache();
        }
        return b ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long ids)
    {

        //如果关联了中间表 > 0， 就不能删除
        //nursingProjectPlan project_id
        List<NursingProjectPlan> lists = nursingProjectPlanMapper.selectByProjectId(ids);


        if(CollectionUtils.isEmpty(lists)) {
            removeByIds(Arrays.asList(ids));

            deleteCache();
        }

        //否则可以删除
        return lists.size();
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        boolean b = removeById(id);
        if (b){
            deleteCache();
        }
        return b ? 1 : 0;
    }

    private void deleteCache() {
        redisTemplate.delete(CACHE_KEY_PREFIX);
    }

    @Override
    public List<NursingProjectVo> selectAll() {

        List<NursingProjectVo> nursingProjects = (List<NursingProjectVo>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if(!CollectionUtils.isEmpty(nursingProjects)){
            return nursingProjects;

        }
        nursingProjects = nursingProjectMapper.selectAll();

        if(!CollectionUtils.isEmpty(nursingProjects)){

            redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, nursingProjects);
        }
        return nursingProjects;

    }
}

